import {
  Toast
} from "vant";
import router from "@/router";
import Config from "@/config";
const baseURL = Config.baseUrl;
const statusName = Config.response.statusName;
const statusCode = Config.response.statusCode;
const msgName = Config.response.msgName;
const dataName = Config.response.dataName;
const logoutUrl = Config.logoutUrl;
const tokenName = Config.request.tokenName;
/* console.log({
  baseURL,
  statusName,
  statusCode,
  msgName,
  dataName,
  logoutUrl,
  tokenName
}); */

class Util {
  constructor() {
    this.baseUrl = baseURL;
    this.code = statusName;
    this.status = statusCode;
    this.message = msgName;
    this.data = dataName;
    this.logout_url = logoutUrl;
    this.token = tokenName;
  }
  setTitle(title) {
    window.document.title = title || "admin";
  }
  /* 获取验证码倒计时 */
  _setTime({
    countdown,
    start,
    end
  }) {
    let nums = parseInt(countdown);
    if (countdown == 0) {
      /* that.sendText = "获取验证码";
      that.sendLock = false; */
      start && start();
      countdown = nums;
      return;
    } else {
      /* that.sendText = `重新发送 ${countdown} 秒`;
      that.sendLock = true; */
      end && end(countdown);
      countdown--;
    }
    setTimeout(() => {
      this._setTime({
        countdown,
        start,
        end
      });
    }, 1000);
  }
  /* 操作成功提示 */
  _doneSuccess({
    res
  }) {
    if (res[this.code] && res[this.code] !== 200) {
      Toast.fail({
        message: res[this.message],
        duration: 1500
      });
      return;
    }
    Toast.success({
      message: "操作成功",
      duration: 1500
    });
  }
  _done({
    res,
    callback,
    to
  }) {
    return new Promise((resolve, reject) => {
      if (res[this.code] && res[this.code] !== 200) {
        Toast.fail({
          message: res[this.message],
          duration: 1500
        });
        reject();
      } else {
        Toast.success({
          message: "操作成功",
          duration: 1500
        });
        callback && callback(res);
        setTimeout(() => {
          router.push({
            path: to
          });
        }, 1500);
        resolve(res);
      }
    });
  }
}

export default Util;